﻿using Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EWPlatAPP.UControls
{
        public partial class UTextBox : TextBox
        {
                public UTextBox()
                {
                        InitializeComponent();
                        SetStyles();
                }

                private void SetStyles()
                {
                        // TextBox由系统绘制，不能设置 ControlStyles.UserPaint样式
                        SetStyle(ControlStyles.UserPaint, true);
                        SetStyle(ControlStyles.AllPaintingInWmPaint, true);
                        SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
                        SetStyle(ControlStyles.ResizeRedraw, true);
                        SetStyle(ControlStyles.SupportsTransparentBackColor, true);
                        UpdateStyles();
                }


                private const int WM_PAINT = 0xF;
                private Color bgColor = Color.Gray;
                /// <summary>
                /// 背景色（渐变色中，颜色1）
                /// </summary>
                [DefaultValue(typeof(Color), "Gray"), Description("控件背景色")]
                public Color BgColor
                {
                        get { return bgColor; }
                        set
                        {
                                bgColor = value;
                                Invalidate();
                        }
                }
                [DefaultValue(typeof(Color), "Transparent"), Description("控件背景颜色2")]
                private Color bgColor2 = Color.Transparent;
                public Color BgColor2
                {
                        get { return bgColor2; }
                        set
                        {
                                bgColor2 = value;
                                Invalidate();
                        }
                }


                private Color borderColor = Color.Gray;
                [DefaultValue(typeof(Color), "Gray"), Description("控件边框颜色")]
                public Color BorderColor
                {
                        get { return borderColor; }
                        set
                        {
                                borderColor = value;
                                Invalidate();
                        }
                }

                private int borderWidth = 0;
                [DefaultValue(typeof(int), "0"), Description("控件边框粗细")]
                public int BorderWidth
                {
                        get { return borderWidth; }
                        set
                        {
                                borderWidth = value;
                                Invalidate();
                        }
                }


                [DefaultValue(typeof(int), "3"), Description("圆角弧度大小")]
                private int radius = 3;
                public int Radius
                {
                        get { return radius; }
                        set
                        {
                                radius = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(LinearGradientMode), "Vertical"), Description("渐变方式")]
                private LinearGradientMode gradientMode = LinearGradientMode.Vertical;
                public LinearGradientMode GradientMode
                {
                        get { return gradientMode; }
                        set
                        {
                                gradientMode = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(int), "100"), Description("文本框宽度")]
                public int UWidth
                {
                        get { return this.Width; }
                        set
                        {
                                this.Width = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(int), "30"), Description("文本框高度")]
                public int UHeight
                {
                        get { return this.Height; }
                        set
                        {
                                this.Height = value;
                                Invalidate();
                        }
                }

                [DefaultValue(typeof(bool), "True"), Description("是否可改变大小")]
                public bool CustomAutoSize
                {
                        get { return this.AutoSize; }
                        set { this.AutoSize = value;

                        }
                }

                protected override void OnGotFocus(EventArgs e)
                {
                        base.OnGotFocus(e);
                        this.BgColor = Color.Gray;
                }

                protected override void OnKeyDown(KeyEventArgs e)
                {
                        base.OnKeyDown(e);
                        this.BgColor = Color.Gray;
                }

                protected override void OnMouseDown(MouseEventArgs e)
                {
                        base.OnMouseDown(e);
                        this.BgColor = Color.Gray;
                }

                protected override void OnMouseClick(MouseEventArgs e)
                {
                        base.OnMouseClick(e);
                        this.BgColor = Color.Gray;
                }
                protected override void WndProc(ref Message m)
                {
                        base.WndProc(ref m);
                        if (m.Msg == WM_PAINT)
                        {
                                //绘制工作：重绘边框、背景
                                using (Graphics g = Graphics.FromHwnd(this.Handle))
                                {
                                        g.SmoothingMode = SmoothingMode.AntiAlias;
                                        Rectangle r = new Rectangle();
                                        r.Width = this.Width;
                                        r.Height = this.Height;
                                        DrawBorder(g, r, this.Radius);//绘制边框和背景还有文本
                                }
                        }
                }

                private void DrawBorder(Graphics g, Rectangle rect, int radius)
                {
                        rect.Width -= 1;
                        rect.Height -= 1;

                        if (BorderWidth > 0)//边框粗细》0
                        {
                                GraphicsPath path = new GraphicsPath();
                                if (radius > 0)
                                {
                                        path = PaintCommon.GetRoundRectangle(rect, radius);//绘制圆角矩形路径
                                        using (Pen pen = new Pen(this.BorderColor, BorderWidth))
                                        {
                                                g.DrawPath(pen, path);
                                        }
                                }
                                else
                                {
                                        using (Pen pen = new Pen(this.BorderColor, BorderWidth))
                                        {
                                                g.DrawRectangle(pen, rect);
                                        }
                                }

                        }
                        //背景区域的矩形
                        Rectangle rect1 = new Rectangle(rect.X + BorderWidth, rect.Y + BorderWidth, rect.Width - 2 * BorderWidth, rect.Height - 2 * BorderWidth);
                        DrawBackColor(g, rect1, radius);// 绘制背景
                                                        //绘制文本
                        if (!string.IsNullOrEmpty(this.Text))
                        {
                                StringFormat format = new StringFormat();
                                if (this.TextAlign == HorizontalAlignment.Left)
                                        format.Alignment = StringAlignment.Near;
                                else if (this.TextAlign == HorizontalAlignment.Right)
                                        format.Alignment = StringAlignment.Far;
                                else
                                        format.Alignment = StringAlignment.Center;
                                format.LineAlignment = StringAlignment.Center;

                                Rectangle rect2 = new Rectangle(5, 2, rect.Width - 10, rect.Height - 4);
                                if(!string.IsNullOrEmpty(this.PasswordChar.ToString()))
                                {
                                        string str = "";
                                        for (int i = 0; i < this.Text.Length; i++)
                                        {
                                                str += this.PasswordChar;
                                        }
                                        g.DrawString(str, this.Font, new SolidBrush(this.ForeColor), rect1, format);
                                }
                                else
                                {
                                        g.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), rect1, format);
                                }
                               
                        }

                }

                /// <summary>
                /// 绘制背景
                /// </summary>
                /// <param name="g"></param>
                /// <param name="rect1"></param>
                /// <param name="radius"></param>
                private void DrawBackColor(Graphics g, Rectangle rect, int radius)
                {
                        //获取背景区域的圆角矩形
                        //g.Clear(BackColor);
                        //线型渐变画刷
                        LinearGradientBrush brush = new LinearGradientBrush(rect, BgColor, BgColor2, gradientMode);
                        Brush b = new SolidBrush(BgColor);
                        if (radius > 0)
                        {
                                GraphicsPath path1 = PaintCommon.GetRoundRectangle(rect, radius);
                                if (this.BgColor2 != Color.Transparent)
                                {
                                        g.FillPath(brush, path1);//填充圆角矩形内部
                                }
                                else
                                {

                                        g.FillPath(b, path1);//填充圆角矩形内部
                                }
                        }
                        else
                        {
                                if (this.BgColor2 != Color.Transparent)
                                {
                                        g.FillRectangle(brush, rect);//填充矩形内部
                                }
                                else
                                {

                                        g.FillRectangle(b, rect);//填充矩形内部
                                }
                        }
                }
        }
}
